// ai_response_model.dart
import 'dart:convert';

class AiResponse {
  final Output output;
  final Usage usage;
  final String requestId;

  AiResponse({
    required this.output,
    required this.usage,
    required this.requestId,
  });

  factory AiResponse.fromJson(Map<String, dynamic> json) {
    return AiResponse(
      output: Output.fromJson(json['output']),
      usage: Usage.fromJson(json['usage']),
      requestId: json['request_id'],
    );
  }
}

class Output {
  final List<Thought> thoughts;
  final String sessionId;
  final String finishReason;
  final String text;
  final bool rejectStatus;

  Output({
    required this.thoughts,
    required this.sessionId,
    required this.finishReason,
    required this.text,
    required this.rejectStatus,
  });

  factory Output.fromJson(Map<String, dynamic> json) {
    var thoughtsList = json['thoughts'] as List;
    List<Thought> thoughts = thoughtsList.map((i) => Thought.fromJson(i)).toList();

    return Output(
      thoughts: thoughts,
      sessionId: json['session_id'],
      finishReason: json['finish_reason'],
      text: json['text'],
      rejectStatus: json['reject_status'],
    );
  }
}

class Thought {
  final String action;
  final String actionInputStream;
  final String arguments;
  final String actionType;
  final dynamic observation; // 可能是字符串或对象
  final String actionName;
  final String? thought;
  final String? response;

  Thought({
    required this.action,
    required this.actionInputStream,
    required this.arguments,
    required this.actionType,
    this.observation,
    required this.actionName,
    this.thought,
    this.response,
  });

  factory Thought.fromJson(Map<String, dynamic> json) {
    return Thought(
      action: json['action'],
      actionInputStream: json['action_input_stream'],
      arguments: json['arguments'],
      actionType: json['action_type'],
      observation: json['observation'],
      actionName: json['action_name'],
      thought: json['thought'],
      response: json['response'],
    );
  }

  // 获取解析后的observation数据
  List<ObservationItem>? getObservationItems() {
    if (observation is String) {
      try {
        // 解析字符串形式的JSON数组
        List<dynamic> list = json.decode(observation);
        return list.map((item) => ObservationItem.fromJson(item)).toList();
      } catch (e) {
        // 如果解析失败，返回null
        return null;
      }
    }
    return null;
  }
}

class ObservationItem {
  final String content;
  final String dataName;
  final bool display;
  final bool enableSpecifiedModel;
  final String id;
  final double rankWeight;
  final int referenceIndex;
  final bool rejectStatus;
  final double score;
  final double scoreWithWeight;
  final String title;
  final bool webSearch;
  final String webSearchLogoUrl;
  final String webSearchUrl;
  final String? dataId;
  final List<String>? images;
  final List<int>? pageNumber;

  ObservationItem({
    required this.content,
    required this.dataName,
    required this.display,
    required this.enableSpecifiedModel,
    required this.id,
    required this.rankWeight,
    required this.referenceIndex,
    required this.rejectStatus,
    required this.score,
    required this.scoreWithWeight,
    required this.title,
    required this.webSearch,
    required this.webSearchLogoUrl,
    required this.webSearchUrl,
    this.dataId,
    this.images,
    this.pageNumber,
  });

  factory ObservationItem.fromJson(Map<String, dynamic> json) {
    return ObservationItem(
      content: json['content'],
      dataName: json['dataName'],
      display: json['display'],
      enableSpecifiedModel: json['enableSpecifiedModel'],
      id: json['id'],
      rankWeight: json['rankWeight'],
      referenceIndex: json['referenceIndex'],
      rejectStatus: json['rejectStatus'],
      score: json['score'],
      scoreWithWeight: json['scoreWithWeight'],
      title: json['title'],
      webSearch: json['webSearch'],
      webSearchLogoUrl: json['webSearchLogoUrl'],
      webSearchUrl: json['webSearchUrl'],
      dataId: json['dataId'],
      images: json['images'] != null ? List<String>.from(json['images']) : null,
      pageNumber: json['pageNumber'] != null ? List<int>.from(json['pageNumber']) : null,
    );
  }
}

class Usage {
  final List<ModelUsage> models;

  Usage({required this.models});

  factory Usage.fromJson(Map<String, dynamic> json) {
    var modelsList = json['models'] as List;
    List<ModelUsage> models = modelsList.map((i) => ModelUsage.fromJson(i)).toList();

    return Usage(models: models);
  }
}

class ModelUsage {
  final int inputTokens;
  final int outputTokens;
  final String modelId;

  ModelUsage({
    required this.inputTokens,
    required this.outputTokens,
    required this.modelId,
  });

  factory ModelUsage.fromJson(Map<String, dynamic> json) {
    return ModelUsage(
      inputTokens: json['input_tokens'],
      outputTokens: json['output_tokens'],
      modelId: json['model_id'],
    );
  }
}
